package com.webcash.CNBoard.Service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webcash.CNBoard.Model.Board;
import com.webcash.CNBoard.Model.EmpBoard;
import com.webcash.CNBoard.Model.EmpComment;
import com.webcash.CNBoard.Repository.BoardRepository;


@Service
public class BoardServiceImpl implements BoardService {

	@Autowired
	BoardRepository boardRepository;
	
	public List<EmpBoard> getEmpBoardListByEmpNo(int empNo, int pageNum) {
		
		return boardRepository.selectEmpBoardListByEmpNo(empNo, pageNum);
	}

	public void addEmpBoardByEmpNo(String id, Board board, int empNo) {
		
		int maxEmpBoardNo = boardRepository.selectEmpBoardEmpBoardNoMaxByEmpNo(empNo);
		
		boardRepository.insertEmpBoardByEmpNo(id, board, empNo, (maxEmpBoardNo + 1));
	}

	public EmpBoard getEmpBoardByEmpNoAndEmpBoardNo(int empNo, int empBoardNo) {
		return boardRepository.selectEmpBoardByEmpNoAndEmpBoardNo(empNo, empBoardNo);
	}

	public void removeEmpBoardByEmpNoAndEmpBoardNo(int empNo, int empBoardNo) {
		EmpBoard empBoard = boardRepository.selectEmpBoardByEmpNoAndEmpBoardNo(empNo, empBoardNo);
		
		boardRepository.deleteEmpBoardByEmpNoAndEmpBoardNo(empNo, empBoardNo, empBoard.getBoardNo());
		
	}

	public int getEmpBoardCountByEmpNo(int empNo) {
		return boardRepository.selectEmpBoardCountByEmpNo(empNo);
	}



	public void updateEmpBoardByBoardNo(Board board, int empBoardNo, int empNo) {
		boardRepository.updateEmpBoardByEmpNo(board, empBoardNo, empNo);		
	}

	public List<EmpComment> getEmpCommentListByBoardNo(int boardNo) {
		return boardRepository.selectEmpCommentListByBoardNo(boardNo);
	}

	public void addEmpCommentByBoardNoAndId(int boardNo, String id, String commentContents) {
		boardRepository.insertEmpCommentByBoardNoAndId(boardNo, id, commentContents);
		EmpBoard board = boardRepository.selectEmpBoardByBoardNo(boardNo);
		int commentCount = board.getCommentCount();
		commentCount++; //commentCount 증가
		boardRepository.updateEmpBoardForCommentCountByBoardNo(boardNo, commentCount);
	}

	public void updateEmpBoardForReadCountByEmpNoAndEmpBoardNo(int empNo,
			int empBoardNo) {
		EmpBoard board = boardRepository.selectEmpBoardByEmpNoAndEmpBoardNo(empNo, empBoardNo);
		int readCount = board.getReadCount();
		readCount++;
		boardRepository.updateEmpBoardForReadCountByBoardNo(board.getBoardNo(), readCount);
	}
	
	
	
	
	

	
}
